{extend name="template/base" /}
{block name="content"}

<head>

    <script type="text/javascript" src="__STATIC__/layui/layui.js"></script>
    <link rel="stylesheet" type="text/css" href="__STATIC__/layui/css/layui.css" />

</head>

<div class="page-container" style="margin-top: 40px;">
    <form class="form form-horizontal" id="form" method="post" action="{:\\think\\Request::instance()->baseUrl()}">
        <input type="hidden" name="id" value="{$vo.id ?? ''}">

        <div class="row cl">
            <label class="form-label col-xs-3 col-sm-3">选择休息时间：</label>
            <div class="formControls col-xs-6 col-sm-6">
                <!--<input type="text" class="input-text" placeholder="时间" name="times" value="{$vo.times ?? ''}" >-->

                <input type="text" class="layui-input" name="times" id="test" style="width: 150px; height: 30px; margin-bottom: 20px;" placeholder="yyyy-MM-dd">

                <ul id="timeul">

                </ul>

            </div>
            <div class="col-xs-3 col-sm-3"></div>
        </div>

        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <button type="submit" class="btn btn-primary radius">&nbsp;&nbsp;提交&nbsp;&nbsp;</button>
                <button type="button" class="btn btn-default radius ml-20" onClick="layer_close();">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
            </div>
        </div>
    </form>
</div>

{/block}
{block name="script"}

<script type="text/javascript" src="__LIB__/Validform/5.3.2/Validform.min.js"></script>

<script>

    var timeArr = [];
    var html = '';

    layui.use('laydate', function(){
        var laydate = layui.laydate;

        //自定义事件
        laydate.render({
            elem: '#test'
            ,trigger: 'click' //采用click弹出
            ,calendar: true,

            ready: function(date){

            },
            done: function(value, date, endDate){


                if(!contains(timeArr,value)){

                    timeArr.push(value);

                    html += '<li  style="list-style:none; float:left; margin-left:5px; margin-bottom: 5px"><div onclick="rec(this)" style="width: 80px; height: 25px;  border: 1px gray solid; text-align: center">'+value+'</div></li>';
                }

                $("#timeul").html(html);

            }
        });

    });



    if("{$vo.times ?? ''}") {

        var jsonstr = '{$vo["times"] ? htmlspecialchars_decode($vo["times"]):""}';
        jsonstr = jsonstr.replace(/[\\\\\/\b\f\n\r\t]/g, '');
        var timeJson = JSON.parse(jsonstr);

        if(timeJson.length){

            for(var i = 0; i<timeJson.length;i++){

                html += '<li  style="list-style:none; float:left; margin-left:5px; margin-bottom: 5px"><div onclick="rec(this)" style="width: 80px; height: 25px;  border: 1px gray solid; text-align: center">'+timeJson[i]+'</div></li>';

            }
        }

        $("#timeul").html(html);

        $('#timeul').find('div').each(function() {

            timeArr.push(this.innerHTML);

        })

    }else{


        console.log("首次打开");

        for(var i = 0; i< 365; i++){

            var d = new Date;

            d.setDate(d.getDate() + i);

            var day=d.getDate();

            var month=d.getMonth() + 1;

            var year=d.getFullYear();

            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }

            if (day >= 1 && day <= 9) {
                day = "0" + day;
            }

            var time = year + "-" + month + "-" + day;

            var dateArray = time.split("-");

            var  date1 = new Date(dateArray[0], parseInt(dateArray[1] - 1), dateArray[2]);

            if(date1.getDay() == 0 || date1.getDay() == 6){

                timeArr.push(time);
                html += '<li  style="list-style:none; float:left; margin-left:5px; margin-bottom: 5px"><div onclick="rec(this)" style="width: 80px; height: 25px;  border: 1px gray solid; text-align: center">'+time+'</div></li>';

            }

        }
        $("#timeul").html(html);


    }

    function contains(arr, obj) {
        var i = arr.length;
        while (i--) {
            if (arr[i] === obj) {
                return true;
            }
        }
        return false;
    };

    function removeByValue(arr, val) {

        for(var i=0; i<arr.length; i++) {
            if(arr[i] == val) {
                arr.splice(i, 1);
                break;
            }
        }
    };

    function rec(message){

        var r=confirm("是否删除?");

        if (r==true)
        {

             removeByValue(timeArr, message.innerHTML);

             message.remove();

             html = $("#timeul").html();

        }
        else
        {

        }

    };

    $("form").submit(function(e){

        var timeJson = {};

        timeJson = timeArr;

        $("#test").val(JSON.stringify(timeJson));

    });

    $(function () {

        $('.skin-minimal input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        });

        $("#form").Validform({
            tiptype: 2,
            ajaxPost: true,
            showAllError: true,
            callback: function (ret){
                ajax_progress(ret);
            }
        });
    })
</script>
{/block}